package net.zjitc.service.impl;

import net.zjitc.context.LocalThreadHolder;
import net.zjitc.mapper.BookMapper;
import net.zjitc.mapper.BookRssHistoryMapper;
import net.zjitc.mapper.RssNotificationMapper;
import net.zjitc.pojo.api.ApiResult;
import net.zjitc.pojo.api.PageResult;
import net.zjitc.pojo.api.Result;
import net.zjitc.pojo.dto.query.extend.BookRssHistoryQueryDto;
import net.zjitc.pojo.dto.query.extend.RssNotificationQueryDto;
import net.zjitc.pojo.entity.Book;
import net.zjitc.pojo.entity.RssNotification;
import net.zjitc.pojo.vo.BookRssHistoryVO;
import net.zjitc.pojo.vo.RssNotificationVO;
import net.zjitc.service.RssNotificationService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

//订阅通知
@Service
public class RssNotificationServiceImpl implements RssNotificationService {

    @Resource
    private RssNotificationMapper rssNotificationMapper;
    @Resource
    private BookRssHistoryMapper bookRssHistoryMapper;
    @Resource
    private BookMapper bookMapper;

    //订阅通知新增
    @Override
    public Result<Void> save(RssNotification rssNotification) {
        rssNotification.setCreateTime(LocalDateTime.now());
        List<RssNotification> rssNotifications = new ArrayList<>();
        rssNotifications.add(rssNotification);
        rssNotificationMapper.save(rssNotifications);
        return ApiResult.success();
    }

    //删除
    @Override
    public Result<Void> batchDelete(List<Integer> ids) {
        rssNotificationMapper.batchDelete(ids);
        return ApiResult.success();
    }

    //修改
    @Override
    public Result<Void> update(RssNotification rssNotification) {
        rssNotificationMapper.update(rssNotification);
        return ApiResult.success();
    }

    //查询
    @Override
    public Result<List<RssNotificationVO>> query(RssNotificationQueryDto dto) {
        List<RssNotificationVO> noticeList = rssNotificationMapper.query(dto);
        Integer totalCount = rssNotificationMapper.queryCount(dto);
        return PageResult.success(noticeList, totalCount);
    }

    //书籍通知，转发消息
    @Override
    public Result<Void> postBook(Integer id) {
        BookRssHistoryQueryDto dto = new BookRssHistoryQueryDto();
        dto.setBookId(id);
        // 书籍的信息，修改状态
        Book book = new Book();
        book.setId(id);
        book.setIsPlanBuy(false);
        book.setPlanBuyTime(null);
        bookMapper.update(book);
        List<BookRssHistoryVO> rssHistoryVOS = bookRssHistoryMapper.query(dto);
        if (CollectionUtils.isEmpty(rssHistoryVOS)) {
            return ApiResult.success();
        }
        List<RssNotification> rssNotifications = new ArrayList<>();
        for (BookRssHistoryVO rssHistoryVO : rssHistoryVOS) {
            RssNotification rssNotification = new RssNotification();
            rssNotification.setUserId(rssHistoryVO.getUserId());
            rssNotification.setContent("你订阅的书籍【" + rssHistoryVO.getBookName() + "】已经上线了，可以借书了");
            rssNotification.setIsRead(false);
            rssNotification.setCreateTime(LocalDateTime.now());
            rssNotifications.add(rssNotification);
        }
        rssNotificationMapper.save(rssNotifications);
        return ApiResult.success();
    }

    //消息全部设置为已读
    @Override
    public Result<List<Void>> readDeal() {
        rssNotificationMapper.readDeal(LocalThreadHolder.getUserId());
        return ApiResult.success();
    }
}
